package com.chen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.mapper.RegionMapper;
import com.chen.pojo.common.PageResult;
import com.chen.pojo.dto.RegionDto;
import com.chen.pojo.entry.Region;
import com.chen.pojo.vo.RegionVo;
import com.chen.service.RegionService;
import com.chen.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/8/17
 * 区域管理业务实现类
 */
@Service
@Transactional
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 分页查询
     *
     * @param pageIndex 起始页
     * @param pageSize  页大小
     * @param name      区域名称
     * @return result
     */
    @Override
    public PageResult<RegionVo> searchList(Integer pageIndex, Integer pageSize, String name) {
        Page<RegionVo> page = new Page<>(pageIndex, pageSize);
        regionMapper.selectPageRegion(page, name);
        return PageUtils.mapperResult(page);
    }

    /**
     * 新增区域
     *
     * @param regionDto 区域信息
     * @return 结果
     */
    @Override
    public Boolean addRegion(RegionDto regionDto) {
        Region region = new RegionVo();
        region.setName(regionDto.getRegionName());
        region.setRemark(regionDto.getRemark());
        regionMapper.insert(region);
        return true;
    }

    /**
     * 更新区域
     *
     * @param regionId  区域id
     * @param regionDto 区域信息
     * @return 更新结果
     */
    @Override
    public Boolean updateRegion(String regionId, RegionDto regionDto) {
        Region region = new RegionVo();
        region.setName(regionDto.getRegionName());
        region.setRemark(regionDto.getRemark());
        region.setId(regionId);
        regionMapper.updateById(region);
        return true;
    }

    /**
     * 删除区域
     *
     * @param regionId 区域id
     * @return 删除结果
     */
    @Override
    public Boolean deleteRegion(String regionId) {
        Region region = new Region();
        region.setId(regionId);
        regionMapper.deleteById(region);
        return true;
    }
}
